set more off
clear all

cap log close
log using an_ipdet,t replace

*** Some statistics on the ENIA-IP sample for 1995-2005
*** Plus probit and Poisson estimates of TM and patenting determinants.
*** Tables B-4, 3, 4
*** BHH June 2017, Augsut 2019

use sample, clear
xtset id year
sort id year

foreach var of varlist dpat dtm { 
  egen ever`var' = sum(`var'),by(id)
  replace ever`var' = ever`var'>0
  gen x = (id==id[_n-1] & ~`var'[_n-1] & `var') | (id~=id[_n-1] & `var')
  replace x = year*x 
  replace x = . if x==0
  egen firstyr = min(x),by(id)
  *replace ever`var' = firstyr~=. & year>=firstyr
  drop x firstyr
  }

keep *new *old logs loge loga logm ind id year foreign mixed public indiv export santiago ///
     hhi share4 shfor napp* d* isic3 ever* minyear

qui tab ind,gen(i_)
qui tab year,gen(y_)
gen logae = loga-loge
gen logse = logs-loge
gen logme = logm-loge
gen dexport = exports>0
gen loghhi = log(hhi)
gen logms = log(share4)
gen logfs = log(shfor)
gen dfsale = logfs==.

global inddum i_2 i_3 i_4 i_5 i_6 i_7 i_8 i_9 i_10 i_11 i_12 i_13 i_14 i_15 i_16 i_17 i_18
global dummies $inddum y_1-y_10
global vars loge logae foreign public indiv dexport santiago logms loghhi logfs dfsale
global indep $vars $dummies
global dep everdtm everdpat dtm tmnew tmold dpat patnew patold
global idummies foreign public indiv dexport santiago dfsale

*** Tables B5 and B6
tabstat loge logse logae logme logms loghhi logfs napp_tm napp_pat, stat(mean sd median p25 p75 min max n) col(stat)
tabstat $idummies $dep, stat(mean sd median p25 p75 min max n) col(stat)
xtsum $idummies $dep

egen nyear = count(year),by(id)
tabstat loge logse logae logme logms loghhi logfs napp_tm napp_pat if nyear>2 & id==id[_n-2], stat(mean sd median p25 p75 min max n) col(stat)
tabstat $idummies $dep if nyear>2 & id==id[_n-2], stat(mean sd median p25 p75 min max n) col(stat)

replace logfs = 0 if dfsale

*** Table 3
qui dprobit everdtm $indep, cluster(id) 
est store probtm
qui poisson napp_tm $indep, cluster(id) 
est store fishtm

*** Table 4
qui dprobit everdpat $vars i_2-i_13 i_15 i_16 i_18 y_1-y_10 if ind~=31 & ind~=35, cluster(id) 
est store probpat
qui poisson napp_pat $vars i_2-i_13 i_15 i_16 i_18 y_1-y_10 if ind~=31 & ind~=35, cluster(id) 
est store fishpat

esttab *tm *pat, keep($vars $inddum) order($vars $inddum) wide nogap             ///
       b(%10.3f) se(%10.3f) nostar stats(r2_p chi2 F df_m N N_clust) 
esttab *tm *pat, keep($vars $inddum) order($vars $inddum) wide nogap             ///
       b(%10.3f) se(%10.3f) star stats(r2_p chi2 F df_m N N_clust) 

label def tmlbl 0 "No TM use" 1 "New TM user" 2 "Old TM user"
label def patlbl 0 "No patent use" 1 "New patent user" 2 "Old patent user"

foreach x in tm pat {
  replace `x'new = everd`x' if ~`x'old
  gen `x'type = `x'new
  replace `x'type = 2 if `x'old
  label val `x'type `x'lbl
  tab `x'type

  sort id year
  tab ind `x'type if id~=id[_n+1]

  mlogit `x'type $indep, cluster(id) base(0)
  stset year, id(id) failure(`x'type=1) origin(minyear)
  qui stcox $indep if `x'type<2, robust
  est store `x'cox
}

esttab *cox, keep($vars $inddum) order($vars $inddum) wide nogap             ///
       b(%10.3f) se(%10.3f) nostar stats(r2_p chi2 F df_m N N_clust) 
esttab *cox, keep($vars $inddum) order($vars $inddum) wide nogap             ///
       b(%10.3f) se(%10.3f) star stats(r2_p chi2 F df_m N N_clust) 
      
log close



